package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.mapper.PersonalMoneyMapper;
import com.hamster.crmpre.pojo.PersonalMoney;
import com.hamster.crmpre.pojo.dto.LineChart;
import com.hamster.crmpre.service.DataChartService;
import com.hamster.crmpre.service.PersonalMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据统计图的Service
 * @author Tarry
 * @create 2020/2/17 13:44
 */
@Service
public class DataChartServiceImpl implements DataChartService {

    @Autowired
    private PersonalMoneyMapper personalMoneyMapper;
    @Autowired
    private PersonalMoneyService personalMoneyService;

    /**
     * 根据id 获取个人的业绩 折线图
     * @param userId
     * @return
     */
    @Override
    public LineChart getPersonalLineChart(String userId) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");//日期格式
        LineChart lineChart = new LineChart();
        lineChart.setXList(new LinkedList<>());
        lineChart.setVList(new LinkedList<>());
        QueryWrapper<PersonalMoney> personalMoneyQueryWrapper = new QueryWrapper<>();
        personalMoneyQueryWrapper.eq("person_id",userId);
        personalMoneyQueryWrapper.orderByAsc("create_time");
        List<PersonalMoney> curPersonalMonies = personalMoneyMapper.selectList(personalMoneyQueryWrapper);
        if (curPersonalMonies == null || curPersonalMonies.size() == 0){
            //如果结果为空，直接返回
            return lineChart;
        }
        Date firstTime = curPersonalMonies.get(0).getCreateTime();//最早的业绩时间
        Date lastTime = curPersonalMonies.get(curPersonalMonies.size() - 1).getCreateTime();//最新的业绩时间
        String firstTimeStr = format.format(firstTime);
        String lastTimeStr = format.format(lastTime);
        Date firstMonth = format.parse(firstTimeStr);
        Date lastMonth = format.parse(lastTimeStr);
        LinkedList<String> xList = new LinkedList<>();
        LinkedList<Double> vList = new LinkedList<>();


        //判断 最早时间，和 最新时间
        for (int i=0;i < 6;i++){
            Date curTime = new Date(lastMonth.getTime()); //临时时间
            lastMonth.setMonth(lastMonth.getMonth() - 1);
            //表示 最早时间，和最新时间是同一个月
            if (lastMonth.getTime() < firstMonth.getTime() && i == 0){
                xList.addFirst(format.format(curTime).substring(2));
                Double money = this.getMoney(userId, curTime);
                vList.addFirst(money);
                lineChart.setVList(vList);
                lineChart.setXList(xList);
                break;
            }
            xList.addFirst(format.format(curTime).substring(2));
            Double money = this.getMoney(userId, curTime);
            vList.addFirst(money);

            if (curTime.getTime() == firstMonth.getTime()){
                break;
            }
        }

        lineChart.setVList(vList);
        lineChart.setXList(xList);
        return lineChart;
    }

    /**
     * 根据月份查询 业绩金额
     * @param userId
     * @param time
     * @return
     */
    private Double getMoney(String userId,Date time){
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        cal.add(Calendar.MONTH,1);
        Date nextMonthTime = cal.getTime();
        Double money = personalMoneyService.personalMoneyCount(userId, time, nextMonthTime);
        return money;
    }





}
